<?php
/**
 * @author Minh Luan
 * @copyright 2012
 */
include_once("../service/CategoryService.php");
include_once("../service/ProductService.php");

if(isset($_POST['pro_name_en']) && isset($_POST['pro_name_vn']) && $_GET['n'] ){
    $pro_id = $_GET["n"];
    $pro_name_en = $_POST['pro_name_en'];
    $pro_name_vn = $_POST['pro_name_vn'];
    $description_en = $_POST['pro_description_en'];
    $description_vn = $_POST['pro_description_vn'];
    $pro_code = $_POST['pro_code'];
    $pro_price = $_POST['pro_price'];
    $cat_parent_id = $_POST['cat_parent_id'];
    $pro_status = $_POST['pro_status'];
	$imgs = getImageOfProduct($pro_id);
	for($i=1; $i <= 4; $i++){
		if($i == 1){
			$img = mysql_fetch_array($imgs);
			$img1ID = $img["IMG_ID"];
		}else if($i == 2){
			$img = mysql_fetch_array($imgs);
			$img2ID = $img["IMG_ID"];
		}else if($i == 3){
			$img = mysql_fetch_array($imgs);
			$img3ID = $img["IMG_ID"];
		}else if($i == 4){
			$img = mysql_fetch_array($imgs);
			$img4ID = $img["IMG_ID"];
		}
	}
	
	
    updateProductInformation($pro_id,$pro_name_en,$pro_name_vn, $pro_code, $description_en,  $description_vn, $pro_price, $cat_parent_id, $pro_status);
            if($_FILES['img1']['name'] != ''){
                //Check valid image type
                if ((($_FILES["img1"]["type"] == "image/gif") || ($_FILES["img1"]["type"] == "image/jpeg") || ($_FILES["img1"]["type"] == "image/jpg")))
                {
                    if ($_FILES["img1"]["error"] > 0)
                    {
                        echo "Return Code: " . $_FILES["img1"]["error"] . "<br />";
                    }
                    else
                    {
                        move_uploaded_file($_FILES["img1"]["tmp_name"],"../images/product/PRO_".$pro_id."/1.jpg");
						updateImageUrl($img1ID,"1.jpg");
						
                    }
                }
                else
                {
                    echo "Invalid file";
                    echo "Upload: " . $_FILES["img1"]["name"] . "<br />";
                    echo "Type: " . $_FILES["img1"]["type"] . "<br />";
                    echo "Size: " . ($_FILES["img1"]["size"] / 1024) . " Kb<br />";
                    echo "Temp file: " . $_FILES["img1"]["tmp_name"] . "<br />"; 
                }   
            }
            if($_FILES['img2']['name'] != ''){
                //Check valid image type
                if ((($_FILES["img2"]["type"] == "image/gif") || ($_FILES["img2"]["type"] == "image/jpeg") || ($_FILES["img2"]["type"] == "image/jpg")))
                {
                    if ($_FILES["img2"]["error"] > 0)
                    {
                        echo "Return Code: " . $_FILES["img2"]["error"] . "<br />";
                    }
                    else
                    {
                        move_uploaded_file($_FILES["img2"]["tmp_name"],"../images/product/PRO_".$pro_id."/2.jpg");
						updateImageUrl($img2ID,"2.jpg");
                    }
                }
                else
                {
                    echo "Invalid file";
                    echo "Upload: " . $_FILES["img2"]["name"] . "<br />";
                    echo "Type: " . $_FILES["img2"]["type"] . "<br />";
                    echo "Size: " . ($_FILES["img2"]["size"] / 1024) . " Kb<br />";
                    echo "Temp file: " . $_FILES["img2"]["tmp_name"] . "<br />"; 
                }  
            } 
            if($_FILES['img3']['name'] != ''){
                //Check valid image type
                if ((($_FILES["img3"]["type"] == "image/gif") || ($_FILES["img3"]["type"] == "image/jpeg") || ($_FILES["img3"]["type"] == "image/jpg")))
                {
                    if ($_FILES["img3"]["error"] > 0)
                    {
                        echo "Return Code: " . $_FILES["img3"]["error"] . "<br />";
                    }
                    else
                    {
                        move_uploaded_file($_FILES["img3"]["tmp_name"],"../images/product/PRO_".$pro_id."/3.jpg");
						updateImageUrl($img3ID,"3.jpg");
                    }
                }
                else
                {
                    echo "Invalid file";
                    echo "Upload: " . $_FILES["img3"]["name"] . "<br />";
                    echo "Type: " . $_FILES["img3"]["type"] . "<br />";
                    echo "Size: " . ($_FILES["img3"]["size"] / 1024) . " Kb<br />";
                    echo "Temp file: " . $_FILES["img3"]["tmp_name"] . "<br />"; 
                }   
            }
            if($_FILES['img4']['name'] != ''){
                //Check valid image type
                if ((($_FILES["img4"]["type"] == "image/gif") || ($_FILES["img4"]["type"] == "image/jpeg") || ($_FILES["img4"]["type"] == "image/jpg")))
                {
                    if ($_FILES["img4"]["error"] > 0)
                    {
                        echo "Return Code: " . $_FILES["img4"]["error"] . "<br />";
                    }
                    else
                    {
                        move_uploaded_file($_FILES["img4"]["tmp_name"],"../images/product/PRO_".$pro_id."/4.jpg");
						updateImageUrl($img4ID,"4.jpg");
                    }
                }
                else
                {
                    echo "Invalid file";
                    echo "Upload: " . $_FILES["img4"]["name"] . "<br />";
                    echo "Type: " . $_FILES["img4"]["type"] . "<br />";
                    echo "Size: " . ($_FILES["img4"]["size"] / 1024) . " Kb<br />";
                    echo "Temp file: " . $_FILES["img4"]["tmp_name"] . "<br />"; 
                }   
            }
            echo "<script>location.href='../admin/product-detail.php?do=view&n=".$pro_id."&msg=5'</script>";  
}else{
    echo "<script>location.href='../admin/product-detail.php?do=view&n=".$pro_id."&msg=6'</script>";
}
?>